<?php

class Application_Form_Zarejestruj extends Zend_Form {

    public function init() {

        $name = new Zend_Form_Element_Text('name', array(
                    'label' => 'Nazwa'
                ));
        $name->setRequired(true);
        $email = new Zend_Form_Element_Text('email', array(
                    'label' => 'Email'
                ));
        $email->setRequired(true);
        $email->addValidator('notEmpty')->addValidator('EmailAddress');

        $password = new Zend_Form_Element_Password('password', array(
                    'label' => 'Hasło'
                ));
        $password->addValidator('identical', false, array('repassword', 'Podaj takie samo hasło'));
        $password->setRequired(true);

        $repassword = new Zend_Form_Element_Password('repassword', array(
                    'label' => 'Powtórz hasło'
                ));

        $login = new Zend_Form_Element_Text('login', array(
                    'label' => 'Login'
                ));
        $login
                ->setRequired(true)
                ->addValidator('notEmpty')
                ->addValidator('stringLength', false, array(3, 16));


        $captcha = new Zend_Form_Element_Captcha('cap', array(
                    'label' => "Przepisz kod z obrazka",
                    'captcha' => 'Image',
                    'captchaOptions' => array(
                        'captcha' => 'Image',
                        'wordLen' => 6,
                        'timeout' => 300,
                        'imgDir' => APPLICATION_PATH . '/../public/img/captcha/',
                        'imgUrl' => '/img/captcha/',
                        'font' => APPLICATION_PATH . '/../public/css/League_Gothic-webfont.ttf',
                    ),
                ));

        
        $this->addDisplayGroup(array(
            $email, $login, $password, $repassword
                ), 'gr-main');
        $this->addDisplayGroup(array(
            $captcha
                ), 'gr-cap');

        $submit = new Zend_Form_Element_Submit('submit', array(
                    'label' => 'Zarejestruj się',
                    'class' => 'ui-form-button subReg'
                ));
        $submit->setDecorators(array('ViewHelper'));

        $helpakt = new Zend_Form_Element_Text('reaktyw', array(
                    'label' => 'Wyślij ponownie link aktywacyjny',
                    'class' => 'fl helppass',
                ));
        $helpakt->setValue($this->getView()->url(array('module' => 'default', 'controller' => 'index', 'action' => 'resendaktyw'), 'default', true));

        $helpakt->setDecorators(array(
            array('ViewScript', array('viewScript' => '_link.phtml'))
        ));

        $this->addDisplayGroup(array(
           $helpakt, $submit
                ), 'gr-submit', array(
            'order' => 999,
            'class' => 'rejestruj'
        ));
    }

    public function isValid($data) {
        $valid = parent::isValid($data);

        $db = Zend_Db_Table::getDefaultAdapter();

        $res = $db->query('select login from users where login like \'' . $data['login'] . '\' or email like \'' . $data['email'] . '\' limit 1')->fetch();
        $rese = $db->query('select login from users where email like \'' . $data['email'] . '\' limit 1')->fetch();
        if ($res != false) {
            $valid = false;
            $this->getElement('login')
                    ->setErrors(array('Podaj inny login!'));
        }
        if ($rese != false) {
            $valid = false;
            $this->getElement('email')
                    ->setErrors(array('Podaj inny e-mail!'));
        }
        return $valid;
    }

}

